<?php
class AjaxUserInformer_ValidateController extends Core_Controller_Action_Standard {
	public function init() {
		$ajaxContext = $this->_helper->getHelper ( 'AjaxContext' );
		$ajaxContext->addActionContext ( 'index', 'json' )->initContext ();
	}
	
	public function indexAction() {
		if ($this->getRequest ()->isPost ()) {
			$ajaxui_users = $this->_getParam ( 'ajaxui_users', "" );
			$ajaxui_users = preg_replace ( "/,{2,}/", ",", $ajaxui_users );
			$ajaxui_users = trim ( preg_replace ( "/[^a-zA-Z0-9,]/", "", $ajaxui_users ), "," );
			$ajaxui_users = array_unique ( explode ( ",", $ajaxui_users ) );
			
			$table = $this->_helper->api ()->getDbtable ( 'users', 'user' );
			
			$select = $table->select ()->from ( $table->info ( 'name' ) );
			
			foreach ( $ajaxui_users as $ajaxui_user ) {
				if (intval ( substr ( $ajaxui_user, 0, 1 ) ) > 0) {
					$select->orWhere ( "user_id = ?", $ajaxui_user );
				} else {
					$select->orWhere ( "username = ?", $ajaxui_user );
				}
			}
			
			$stmt = $select->query ();
			$result = $stmt->fetchAll ();
			
			$settings = Engine_Api::_ ()->getApi ( 'settings', 'MsepCore' );
			
			$viewer = Engine_Api::_ ()->user ()->getViewer ();
			
			$friend = new User_Plugin_Menus ();
			$message = new Messages_Plugin_Menus ();
			
			$final = array ();
			
			for($i = 0; $i < count ( $result ); $i ++) {
				foreach ( $ajaxui_users as $ajaxui_user ) {
					$user_identity = ((intval ( substr ( $ajaxui_user, 0, 1 ) ) > 0) ? true : false);
					
					if (($user_identity && $result [$i] ['user_id'] == $ajaxui_user) || (! $user_identity && $result [$i] ['username'] == $ajaxui_user)) {
						$subject = Engine_Api::_ ()->user ()->getUser ( $result [$i] ['username'] );
						
						if (! $viewer->isSelf ( $subject ) && $subject->authorization ()->isAllowed ( $viewer, 'view' )) {
							$identifier = ((intval ( substr ( $ajaxui_user, 0, 1 ) ) > 0) ? $result [$i] ['user_id'] . "_user" : $result [$i] ['username']);
							
							$view = $this->view;
							
							Engine_Api::_ ()->core ()->clearSubject ();
							Engine_Api::_ ()->core ()->setSubject ( $subject );
							
							$final [$identifier] = $result [$i];
							$final [$identifier] ['mutual'] ['total'] = 0;
							
							$final [$identifier] ['button'] ['friend'] = "";
							$final [$identifier] ['button'] ['message'] = "";
							
							if (Engine_Api::_ ()->user ()->getAuth ()->hasIdentity ()) {
								if ($settings->ajaxui->user_button_friend == 1) {
									$params = $friend->onMenuInitialize_UserProfileFriend ( null );
									
									if (! empty ( $params )) {
										$href = Zend_Controller_Front::getInstance ()->getRouter ()->assemble ( array ('action' => $params ['params'] ['action'], 'controller' => $params ['params'] ['controller'], 'user_id' => $params ['params'] ['user_id'] ), $params ['route'], true );
										
										$final [$identifier] ['button'] ['friend'] = $view->htmlLink ( $href, null, array ('alt' => $params ['label'], 'title' => $params ['label'], 'class' => 'buttonlink smoothbox menu_user_profile', 'style' => 'margin-right: 8px; padding: 8px; background-image: url(\'' . $params ['icon'] . '\');' ) );
									}
								}
								
								if ($settings->ajaxui->user_button_message == 1) {
									$params = $message->onMenuInitialize_UserProfileMessage ( null );
									
									if (! empty ( $params )) {
										$href = Zend_Controller_Front::getInstance ()->getRouter ()->assemble ( array ('action' => $params ['params'] ['action'], 'to' => $params ['params'] ['to'] ), $params ['route'], true );
										
										$final [$identifier] ['button'] ['message'] = $view->htmlLink ( $href, null, array ('alt' => $params ['label'], 'title' => $params ['label'], 'class' => 'buttonlink menu_user_profile', 'style' => 'margin-right: 8px; padding: 8px; background-image: url(\'' . $params ['icon'] . '\');' ) );
									}
								}
								
								$final [$identifier] ['mutual'] ['friends'] = array ();
								$final [$identifier] ['mutual'] ['total'] = 0;
								
								if ($settings->ajaxui->user_mutual == 1) {
									$friendsTable = Engine_Api::_ ()->getDbtable ( 'membership', 'user' );
									$friendsName = $friendsTable->info ( 'name' );
									$select = new Zend_Db_Select ( $friendsTable->getAdapter () );
									
									$select->from ( $friendsName, 'user_id' )->join ( $friendsName, "`{$friendsName}`.`user_id`=`{$friendsName}_2`.user_id", null )->where ( "`{$friendsName}`.resource_id = ?", $viewer->getIdentity () )->where ( "`{$friendsName}_2`.resource_id = ?", $subject->getIdentity () )->where ( "`{$friendsName}`.active = ?", 1 )->where ( "`{$friendsName}_2`.active = ?", 1 );
									
									$selectResult = $select->query ()->fetchAll ();
									
									$final [$identifier] ['mutual'] ['total'] = 0;
									
									if (count ( $selectResult ) > 0) {
										$usersTable = Engine_Api::_ ()->getDbtable ( 'users', 'user' );
										$usersName = $usersTable->info ( 'name' );
										$selectUsers = new Zend_Db_Select ( $usersTable->getAdapter () );
										
										$selectUsers->from ( $usersName, array ('user_id', 'photo_id' ) );
										
										$j = 0;
										
										foreach ( $selectResult as $data ) {
											$selectUsers->orWhere ( "user_id = ?", $data ['user_id'] );
											
											if ($j == 3)
												break;
											
											$j ++;
										}
										
										foreach ( $selectUsers->query ()->fetchAll () as $userInfo ) {
											$userSubject = Engine_Api::_ ()->user ()->getUser ( $userInfo ['user_id'] );
											
											if ($userInfo ['photo_id'] == 0 || ! ($user_photo = $userSubject->getPhotoUrl ( 'thumb.icon' ))) {
												$no_photo = new Core_View_Helper_ItemPhoto ();
												$user_photo = $no_photo->getNoPhoto ( $userSubject, 'thumb_icon' );
											}
											
											$final [$identifier] ['mutual'] ['friends'] [] = array ('photo' => $this->view->htmlLink ( $userSubject->getHref (), "<img src='{$user_photo}' style='padding: 2px; float: left;' border='0' />", array ('alt' => $userSubject->getTitle (), 'title' => $userSubject->getTitle () ) ) );
										}
										
										$final [$identifier] ['mutual'] ['total'] = count ( $final [$identifier] ['mutual'] ['friends'] );
									}
								}
							}
							
							$view->addHelperPath ( APPLICATION_PATH . '/application/modules/AjaxUserInformer/View/Helper', 'AjaxUserInformer_View_Helper_UserFields' );
							
							$fieldStructure = Engine_Api::_ ()->fields ()->getFieldsStructurePartial ( $subject );
							
							$final [$identifier] ['html'] = $view->userFields ( $subject, $fieldStructure );
							
							if (! ($user_photo = $subject->getPhotoUrl ( 'thumb.profile' )) || ! $settings->ajaxui->user_thumbnail) {
								$final [$identifier] ['photo'] = "";
							} else {
								$final [$identifier] ['photo'] = $user_photo;
							}
						}
					}
				}
			}
			
			$view->ajaxui_users = $final;
		}
	}
}